<template>
  <div class="page_config app-container" v-loading="loading">
    <cfCard title='' class='page-container-search bottom12'>
      <div slot='content'>
        <el-form :model="searchParams" ref="searchParams" size="small" :inline="true" label-width="68px">
          <el-form-item label="参数名称" prop="configName">
            <el-input v-model="searchParams.configName" placeholder="请输入参数名称" clearable style="width: 240px" v-search="handleSearch"/>
          </el-form-item>
          <el-form-item label="参数键名" prop="configKey">
            <el-input v-model="searchParams.configKey" placeholder="请输入参数键名" clearable style="width: 240px" v-search="handleSearch" />
          </el-form-item>
          <el-form-item label="系统内置" prop="configType">
            <el-select v-model="searchParams.configType" placeholder="系统内置" clearable @change="handleSearch">
              <el-option v-for="dict in sysYesNo" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间" prop="createTime">
            <el-date-picker
              v-model="searchParams.createTime"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @change="handleSearch"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleSearch">查询</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="handleReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </cfCard>
    <cfCard title='参数列表' hasTable :tableObj='tableDataObj' class='page_house-container-bottom'>
      <!--表格区域-->
      <template slot='btn'>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:config:add']">新增</el-button>
          <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['system:config:edit']" >修改</el-button>
          <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['system:config:remove']" >删除</el-button>
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['system:config:export']">导出</el-button>
          <el-button type="danger" plain icon="el-icon-refresh" size="mini" @click="handleRefreshCache" v-hasPermi="['system:config:remove']">刷新缓存</el-button>
      </template>
      <template slot='content'>
        <cfTable ref="cfTable" :tableDataObj='tableDataObj' :tableData='tableData' @handleSelectionChange="handleSelectionChange">
          <template slot="configType">
            <el-table-column label='系统内置' width='100px' align='center'>
              <dict-tag slot-scope="scope" :options="sysYesNo" :value="scope.row.configType"/>
            </el-table-column>
          </template>
          <template slot="createTime">
            <el-table-column label='创建时间' width='180px'>
              <span slot-scope="scope">{{ parseTime(scope.row.createTime) }}</span>
            </el-table-column>
          </template>
          <template slot='operate'>
            <el-table-column label='操作' width='120px' align='right'>
              <span slot-scope='scope'>
                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:config:edit']">修改</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:config:remove']">删除</el-button>
              </span>
            </el-table-column>
          </template>
        </cfTable>
        <pagination :total='queryParams.total' :page.sync='queryParams.pageNum' :limit.sync='queryParams.pageSize' @pagination='getListFn'/>
      </template>
    </cfCard>
    <updateDialog v-if="dialogObj.visible" :dialogObj="dialogObj" @refresh="getListFn"></updateDialog>
  </div>
</template>

<script>
import { listConfig, getConfig, delConfig, addConfig, updateConfig, refreshCache, exportConfig } from "@/api/system/config";
import updateDialog from "./updateDialog.vue";
export default {
  name: "Config",
  components: {updateDialog},
  data() {
    return {
      loading: true,/**遮罩层*/ 
      ids: [], /**选中数组*/ 
      single: true,  /**非单个禁用*/ 
      multiple: true,  /**非多个禁用*/ 
      tableData: [],/**参数表格数据*/ 
      dialogObj: {
        visible: false,
        id: '',
        title: '',
        width: '538px'
      },
      searchParams: {/**查询参数*/ 
        configName: undefined,
        configKey: undefined,
        configType: undefined,
        createTime: []
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      tableDataObj: {
        id: 'id',
        showSelection: true,
        defaultExpandAll: true,
        treeProps: {children: 'children', hasChildren: 'hasChildren'},
        option: [
          {id: 1, label: '参数主键', prop: 'configId', align: 'center', width: 100, show: 1, needSlot: false},
          {id: 2, label: '参数名称', prop: 'configName', show: 1, needSlot: false},
          {id: 3, label: '参数键名', prop: 'configKey', align: 'center', show: 1, needSlot: false},
          {id: 4, label: '参数键值', prop: 'configValue', align: 'center', show: 1, needSlot: false},
          {id: 5, label: '系统内置', prop: 'configType', show: 1, needSlot: true},
          {id: 6, label: '备注', prop: 'remark', show: 1, needSlot: false},
          {id: 7, label: '创建时间', prop: 'createTime', show: 1, needSlot: true},
        ]
      },
    };
  },
  async created() {
    await this.getSysYesNo();
    this.getListFn();
  },
  methods: {
    async getListFn() {  /** 查询参数列表 */
      this.loading = true;
      const data = {
        ...this.searchParams,
        ...this.queryParams
      }
      delete data.createTime;
      if(Array.isArray(data.createTime) && data.createTime.length) {
        data.params = {
          beginTime: this.searchParams.createTime[0],
          endTime: this.searchParams.createTime[1]
        }
      } 
      const res = await listConfig(data);
      if(res.code == 200) {
        this.tableData = res.rows;
        this.queryParams.total = res.total;
      }
      setTimeout(() => {
        this.loading = false;
        }, 500);
    },
    handleSearch() {  /** 搜索按钮操作 */
      this.queryParams.pageNum = 1;
      this.queryParams.total = 0;
      this.tableData = [];
      this.getListFn();
    },
    handleReset() {  /** 重置按钮操作 */
      this.resetForm("searchParams", ()=>{
        this.handleSearch();
      });
    },
    handleAdd() {  /** 新增按钮操作 */
      this.dialogObj.visible = true;
      this.dialogObj.title = "添加参数";
      this.dialogObj.id = "";
    },
    handleSelectionChange(selection) { /**多选框选中数据*/ 
      this.ids = selection.map(item => item.configId)
      this.single = selection.length!=1;
      this.multiple = !selection.length;
    },
    handleUpdate(row) {  /** 修改按钮操作 */
      const configId = row.configId || this.ids
      this.dialogObj.visible = true;
      this.dialogObj.title = "修改参数";
      this.dialogObj.id = configId;
    },
    handleDelete(row) {  /** 删除按钮操作 */
      const configIds = row.configId || this.ids;
      this.$modal.confirm('是否确认删除参数编号为"' + configIds + '"的数据项？').then(async ()=> {
        this.loading = true;
        const res = await delConfig(configIds);
        if(res.code == 200) {
          this.handleSearch();
          this.$modal.msgSuccess("删除成功");
        }
        setTimeout(() => {
          this.loading = false;
        }, 500);
      }).catch(() => {});
    },
    async handleExport() {  /** 导出按钮操作 */
      const res = await exportConfig(this.searchParams);
      if(res.data && res.status == 200) {
        this.downloadFilesFn(res, `config_${this.formatDate()}.xlsx`)
      }
    },
    handleRefreshCache() {  /** 刷新缓存按钮操作 */
      refreshCache().then(() => {
        this.$modal.msgSuccess("刷新成功");
      });
    }
  }
};
</script>
